package com.meij.activity.user;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import cn.pedant.SweetAlert.SweetAlertDialog;

import com.anno.ActionBarSet;
import com.anno.ActionBarSet.WAY;
import com.meij.MJApplaction;
import com.meij.R;
import com.meij.activity.MainActivity;
import com.meij.activity.MjActivity;
import com.meij.bean.Person;
import com.meij.http.Json;
import com.meij.http.MJNetCallBack;
import com.meij.http.Url;
import com.meij.util.Info;

@ActionBarSet(title = "注册", way = WAY.TITLE, homeAsUpEnabled = true)
public class RegistActivity extends MjActivity {
	// 注册信息相关
	private String phone, code, nick, pass;
	private int sex = 1;
	// 摇晃提示动画
	private Animation shake;
	// 控件
	private EditText edite1, edite2, edite3, edite4, edite5;
	private Button button1;
	private RadioGroup radioGroup;
	// 验证码倒计时时间
	private int countDown = 60;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_regist);
		initView();
	}

	/***
	 * 初始化UI
	 */
	private void initView() {
		shake = AnimationUtils.loadAnimation(this, R.anim.shake);
		edite1 = (EditText) findViewById(R.id.editText1);
		edite2 = (EditText) findViewById(R.id.editText2);
		edite3 = (EditText) findViewById(R.id.editText3);
		edite4 = (EditText) findViewById(R.id.editText4);
		edite5 = (EditText) findViewById(R.id.editText5);
		button1 = (Button) findViewById(R.id.button1);
		radioGroup = (RadioGroup) findViewById(R.id.rg);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.rb1) {
					sex = 1;
				} else if (checkedId == R.id.rb2) {
					sex = 2;
				}
			}
		});
	}

	/***
	 * 手机号校验
	 * 
	 * @return
	 */
	private boolean checkPhone() {
		phone = edite1.getText().toString();
		if (TextUtils.isEmpty(phone)) {
			Toast(R.string.sign_numhit);
			edite1.requestFocus();
			edite1.startAnimation(shake);
			return false;
		}
		return true;
	}

	/***
	 * 获取验证码
	 * 
	 * @param view
	 */
	public void getCode(View view) {
		if (checkPhone()) {
			String url = Url.vcode + "?mobile=" + phone;
			MJNetCallBack callBack = new MJNetCallBack(this) {
				@Override
				public void onSuccess(JSONObject object) {
					super.onSuccess(object);
					button1.setEnabled(false);
					button1.setText(countDown + "s");
					edite2.requestFocus();
					countDown();
					code = Json.getVcode(object);
				}
			};
			getJson(url, callBack);
		}
	}

	/***
	 * 倒计时
	 */
	private void countDown() {
		button1.postDelayed(new Runnable() {
			@Override
			public void run() {
				if (!isFinishing()) {
					countDown--;
					if (countDown > 0) {
						countDown();
						button1.setText(countDown + "s");
					} else {
						countDown = 60;
						button1.setText(R.string.sign_getcode);
						button1.setEnabled(true);
					}
				}
			}
		}, 1000);
	}

	/***
	 * 其他信息校验
	 * 
	 * @return
	 */
	private boolean checkInfo() {
		String typecode = edite2.getText().toString();
		if (TextUtils.isEmpty(typecode)) {
			Toast(R.string.sign_codehit);
			edite2.requestFocus();
			edite2.startAnimation(shake);
			return false;
		}
		nick = edite3.getText().toString();
		if (TextUtils.isEmpty(nick)) {
			Toast(R.string.sign_nickhit);
			edite3.requestFocus();
			edite3.startAnimation(shake);
			return false;
		}
		pass = edite4.getText().toString();
		if (TextUtils.isEmpty(pass)) {
			Toast(R.string.sign_passhit);
			edite4.requestFocus();
			edite4.startAnimation(shake);
			return false;
		}
		String repass = edite5.getText().toString();
		if (TextUtils.isEmpty(repass)) {
			Toast(R.string.sign_repasshit);
			edite5.requestFocus();
			edite5.startAnimation(shake);
			return false;
		}
		if (!pass.equals(repass)) {
			Toast("密码与确认密码不符");
			edite5.requestFocus();
			edite5.startAnimation(shake);
			return false;
		}
		if (!typecode.equals(code)) {
			Toast("验证码错误");
			edite2.requestFocus();
			edite2.startAnimation(shake);
			return false;
		}
		return true;
	}

	/***
	 * 提交数据
	 * 
	 * @param view
	 */
	public void submit(View view) {
		if (checkPhone() && checkInfo()) {
			String url = Url.regist + "?mobile=" + phone + "&pwds=" + pass + "&nickname=" + nick + "&sex=" + sex;
			MJNetCallBack callBack = new MJNetCallBack(this) {
				@Override
				public void onSuccess(JSONObject object) {
					super.onSuccess(object);
					MJApplaction.userInfo = Json.getUserInfo(object);
					// 保存帐号
					MJApplaction.userInfo.setAccount(phone);
					MJApplaction.userInfo.setPerson(new Person());
					MJApplaction.userInfo.getPerson().setNickname(nick);
					Info.saveUserInfo(RegistActivity.this);
					showAlert();
				}
			};
			getJson(url, callBack);
		}
	}

	/***
	 * 弹出提示
	 */
	private void showAlert() {
		new SweetAlertDialog(this, SweetAlertDialog.SUCCESS_TYPE).setTitleText("注册成功").setCancelText("跳过").setConfirmText("完善信息")
				.showCancelButton(true).setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
					@Override
					public void onClick(SweetAlertDialog sDialog) {
						Intent intent = new Intent(RegistActivity.this, MainActivity.class);
						startActivity(intent);
						sDialog.dismiss();
						finish();
					}
				}).setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
					@Override
					public void onClick(SweetAlertDialog sDialog) {
						Intent intent = new Intent(RegistActivity.this, UpdateActivity.class);
						startActivity(intent);
						sDialog.dismiss();
						finish();
					}
				}).show();
	}
}
